package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysacl.dto.SysAclDTO;
import com.talkweb.newproject.admin.sysacl.service.SysAclService;
import com.talkweb.newproject.api.model.admin.SysAclParam;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver;

import java.util.List;

/**
 * Created by qsf on 2019/5/7.
 */
@RestController
@RequestMapping(value="/api/sysAcl")
@Slf4j
public class SysAclRest {
    @Autowired
    private SysAclService sysAclService;

    /**
     * 根据id查询对象
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/get/v1/{id}",method = RequestMethod.POST)
    public ResponseResult<SysAclDTO> querySysAclByPrimaryKey(@PathVariable("id") String id)  throws Exception{
        ResponseResult<SysAclDTO> result = new ResponseResult<>();
        try {
            SysAclDTO entity = sysAclService.querySysAclByPrimaryKey(id);
            result.setData(entity);
            result.setStatus(Status.SUCCESS);
            result.setMessage("查询成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试!");
            return result;
        }
    }

    /**
     * 根据条件查询列表
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getSysAclList",method = RequestMethod.POST)
    public ResponseResult<List<SysAclDTO>> querySysAclList(SysAclParam params)   throws Exception{
        ResponseResult<List<SysAclDTO>> result = new ResponseResult<>();
        List<SysAclDTO> sysAclDTOList = null;
        try {
            sysAclDTOList = sysAclService.searchSysAcl(params);
            result.setStatus(Status.SUCCESS);
            result.setData(sysAclDTOList);
            result.setMessage("查询成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 根据条件查询对象
     * @param sysAcl
     * @return
     */
    @ResponseBody
    @RequestMapping(value="queryLike",method=RequestMethod.POST)
    public ResponseResult<SysAclDTO> queryLike(@RequestBody SysAclDTO sysAcl)   throws Exception{
        ResponseResult<SysAclDTO> result = new ResponseResult<>();
        try {
            SysAclDTO dto   = sysAclService.queryLikeSysAcl(sysAcl);
            result.setStatus(Status.SUCCESS);
            result.setData(dto);
            result.setMessage("查询成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }


    /**
     * 新增数据
     * @param obj
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/create/v1",method = RequestMethod.POST)
    public ResponseResult create(@RequestBody SysAclDTO obj)   throws Exception{
        ResponseResult result = new ResponseResult();
        try {
            sysAclService.insertSysAcl(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("保存成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 修改对象
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/update/v1",method=RequestMethod.POST)
    public ResponseResult update(@RequestBody SysAclDTO obj)   throws Exception{
        ResponseResult result = new ResponseResult();
        try{

            sysAclService.updateSysAcl(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("修改成功");
            return result;
        } catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试!");
            return result;
        }

    }

    @ResponseBody
    @RequestMapping(value="/delete/v1",method = RequestMethod.POST)
    public ResponseResult deleteSysAcl(@RequestParam("ids") String ids) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            if(ids!= null && !"".equals(ids)) {
                sysAclService.deleteSysAcl(ids);
                result.setMessage("删除成功");
                result.setStatus(Status.SUCCESS);
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");
            }
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }
}
